package cn.gson.boot.model.service;

import cn.gson.boot.model.dao.SellDao;
import cn.gson.boot.model.dao.ShenHeDao;
import cn.gson.boot.model.mapper.KehuMapper;
import cn.gson.boot.model.mapper.SellMapper;
import cn.gson.boot.model.mapper.UserMapper;
import cn.gson.boot.model.pojos.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.servlet.http.HttpSession;
import java.math.BigDecimal;
import java.sql.Timestamp;
import java.util.*;

@Service
@Transactional(rollbackFor = Exception.class)
public class SellService {
    @Autowired
    private SellMapper sellMapper;

    @Autowired
    private SellDao sellDao;

    @Autowired
    ShenHeDao shenHeDao;//审核dao

    @Autowired
    private KehuMapper kehuMapper;

    @Autowired
    private UserMapper userMapper;

    //分页查询和模糊查询（查询用mapper）
    public List<Sell> allSell(String sellName ,String sellType,String empName){ return sellMapper.allSell(sellName,sellType,empName); }

    //详情查询通过id
    public Sell xqSell(Integer sellId){
        return sellMapper.xqSell(sellId);
    }

    //确认按钮（新增和修改）
    public void addSell(Sell sell){
        System.out.println("新增了");
        int id = sell.getSellId();
        System.out.println("编号："+id);
        if(id==0){
            sell.setSellztai(1);
            Sell save = sellDao.save(sell);
            //新增审核表
            Shenhe shenhe = new Shenhe();
            shenhe.setShTabid(save.getSellId());
            shenhe.setShShouming("销售费用" );
            shenhe.setChuangJiantime(new Timestamp(new Date().getTime()));
            shenhe.setShZhuangtai("待审核");
            shenhe.setEmp(null);
            System.err.println("申请人"+save.getSellName());
            shenhe.setShenqingPeople(save.getSellName());

            shenHeDao.save(shenhe);
        }else{
            System.out.println("修改了");
            Sell s1 = sellDao.findById(id).get();//找到修改的id
            s1.setSellType(sell.getSellType());//费用类别
            s1.setSellTime(sell.getSellTime());//日期
            s1.setSellApprover(sell.getSellApprover());//审核人
            s1.setSellBeiz(sell.getSellBeiz());//备注
            s1.setSellMoney(sell.getSellMoney());//金额
            s1.setSellPjzs(sell.getSellPjzs());//票据张数
            s1.setSellYongt(sell.getSellYongt());//用途
            s1.setSellRshu(sell.getSellRshu());//人数
            s1.setkId(sell.getkId());//客户对象
            sellDao.save(s1);
        }
    }

    //逻辑删除
    public void deleteSell(int id){
        Sell s3 = sellDao.findById(id).get();//找到删除的id
        s3.setSellztai(0);
        sellDao.save(s3);
    }

    //统计图销售费用类型数量统计
    public Map<String,Object> sellleixing(String empName){
        List<Sell> sells = sellMapper.quanSell(empName);
        Map<String,Object> map = new HashMap<>();
        List<Integer> sj = new ArrayList<>();//数量
        List<String> mz = new ArrayList<>();//名字
        for(int i=0;i<sells.size();i++){
            sj.add(sellMapper.sellleixing(sells.get(i).getSellType(),empName));
            mz.add(sells.get(i).getSellType());
        }
        map.put("sj",sj);
        map.put("mz",mz);
        return map;
    }

    //销售费用 员工分布统计
    public Map<String,Object> kehufuzeren(String empName){
        List<Sell> sells = sellMapper.empNameSell(empName);
        Map<String,Object> map = new HashMap<>();
        List<Integer> sj = new ArrayList<>();
        List<String> mz = new ArrayList<>();
        for(int i=0;i<sells.size();i++){
            sj.add(sellDao.kehufuzeren(sells.get(i).getSellName()));
            mz.add(sells.get(i).getSellName());
        }
        map.put("sj",sj);
        map.put("mz",mz);
        return map;
    }

    //销售费用类别金额统计
    public Map<String,Object> kehufyjine(String empName){
        List<Sell> sells = sellMapper.quanSell(empName);
        Map<String,Object> map = new HashMap<>();
        List<BigDecimal> sj = new ArrayList<>();//金额
        List<String> mz = new ArrayList<>();//名字

        for(int i=0;i<sells.size();i++){
            sj.add(sellMapper.selllxje(sells.get(i).getSellType(),empName));
            mz.add(sells.get(i).getSellType());
        }
        map.put("sj",sj);
        map.put("mz",mz);
        return map;
    }

    //销售费用客户费用前三统计
    public Map<String,Object> kehukhfy(String empName){
        List<Sell> sells = sellMapper.kNameSell(empName);
        Map<String,Object> map = new HashMap<>();
        List<BigDecimal> sj = new ArrayList<>();//金额
        List<String> mz = new ArrayList<>();//名字
        for(int a = 0;a<sells.size();a++){
            sj.add(sellDao.kehukhfy(sells.get(a).getkId()));
            mz.add(sells.get(a).getkId().getkName());
        }
        map.put("sj",sj);
        map.put("mz",mz);
        return map;
    }
}